/*
 * PaperJetsLogic.cpp
 *
 *  Created on: 03.02.2013
 *      Author: Kirill Blinov
 *   Copyright: LGPL
 */

#include "PaperJetsLogic.h"
#include "../../../Manager Resource/ManagerResource.h"
#include "../../../BasicTypes.h"
#include "../../../Gui/GUIBase.h"
#include "../../../Visualisator/Visualisator.h"

PaperJetsLogic::PaperJetsLogic( CGameWorld::CGameWorldUnitIterator* UnitIterator,
                                std::vector<CGameCamera*> MainCamera ) :
       IGameLogic( UnitIterator ), letKernelWork(true)
{
	//Only one camera is supported by PaperJetsLogic
	assert( MainCamera.size() == 1 );
	gameCamera = MainCamera[0];

	memset( (void*)keyTranslationMap, 0, sizeof(SDLKey) * SDLK_LAST );
	CResourceLoader* ResourceLoader = ResourceOwner::GetResourceLoader();
	ResourceLoader->LoadGameLogicConfigFromFile( this, std::vector<std::string>( 1, "content/Logic/KeyMap.txt" ) );
}

PaperJetsLogic::~PaperJetsLogic()
{

}

void
PaperJetsLogic::ProcessInputMessage( const INPUT_MESSAGE &Message )
{
	ProcessKeys( Message.keys );
	letKernelWork = !Message.terminateApplication;
}

void
PaperJetsLogic::ProcessUIMessage( const UI_MESSAGE &Message )
{

}

void
PaperJetsLogic::ProcessKernelEvent( )
{

}

bool
PaperJetsLogic::OnKernelFrame()
{
	return letKernelWork;
}

void
PaperJetsLogic::ProcessKeys( std::vector<SDLKey> Keys )
{
	for ( std::vector<USHORT>::const_iterator it = Keys.begin(),
		  end = Keys.end();
		  it != end; ++it)
	{
		USHORT Key = *it.base();
		switch (Key)
		{
			break;case SDLK_q:
			{
				letKernelWork = false;
			}
			break;case SDLK_w:
			{
				gameCamera->Translate( -0.1, 0.0, 0.0 );
			}
			break;case SDLK_s:
			{
				gameCamera->Translate( 0.1, 0.0, 0.0 );
			}
			break;case SDLK_a:
			{
				gameCamera->Translate( 0.0, 0.1, 0.0 );
			}
			break;case SDLK_d:
			{
				gameCamera->Translate( 0.0, -0.1, 0.0 );
			}
			break;case SDLK_e:
			{
			}
			break;case SDLK_z:
			{
				gameCamera->RotateCenterAroundEye( 0.0, 0.0, 1.0 );
			}
			break;case SDLK_x:
			{
				gameCamera->RotateCenterAroundEye( 0.0, 0.0, -1.0 );
			}
			break;case SDLK_c:
			{
				gameCamera->RotateCenterAroundEye( 0.0, 1.0, 0.0 );
			}
			break;case SDLK_v:
			{
				gameCamera->RotateCenterAroundEye( 0.0, -1.0, 0.0 );
			}
			break;case SDLK_b:
			{
				gameCamera->RotateCenterAroundEye( 1.0, 0.0, 0.0 );
			}
			break;case SDLK_n:
			{
				gameCamera->RotateCenterAroundEye( -1.0, 0.0, 0.0 );
			}
			break;case SDLK_UP:
			{
			}
			break;case SDLK_DOWN:
			{
			}
			break;case SDLK_LEFT:
			{
			}
			break;case SDLK_RIGHT:
			{
			}
			break;default:
			{
			}
			break;
		}
	}
}

void
PaperJetsLogic::ProcessMouse( glm::vec2 MouseDelta, glm::vec2 MousePosition, USHORT MouseKeys[MOUSE_KEY_COUNT] )
{
	static USHORT  PrevLeftButtonState = MOUSE_KEY_UP;

	if ( ( MouseDelta.x != 0 ) ||
		 ( MouseDelta.y != 0 ) ||
		 ( PrevLeftButtonState != MouseKeys[MOUSE_BUTTON_LEFT] ) )
	{
		CManagerGUI*     GuiManager = CManagerGUI::GetInstance();
		CVisualisator*   Visualisator = CVisualisator::GetVisualisator();

		glm::vec2        LogicMousePosition;
		SCREEN_DIMENSION ScreenDimension = Visualisator->GetDimension();

		LogicMousePosition.x = float( MousePosition.x ) / float( ScreenDimension.width );
		LogicMousePosition.y = ( 1.0f - float( MousePosition.y ) / float( ScreenDimension.height ) );
		PrevLeftButtonState = MouseKeys[MOUSE_BUTTON_LEFT];
		GuiManager->EventMouse( MouseKeys[MOUSE_BUTTON_LEFT], LogicMousePosition );
	}
}
